package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 杨林
 * @create 2022-11-03 16:27 星期四
 * description:
 */
@RestController
//@CrossOrigin  //通过网关解决跨域问题
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    //根据数据id查询子数据列表
    @GetMapping("/findChildData/{id}")
    public R findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return R.ok().data("list",list);
    }

    /**
     * 导出数据
     */
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response){
        dictService.exportData(response);
    }

    /**
     * 导入数据
     */
    @PostMapping("/importData")
    public R importData(MultipartFile file){
        dictService.importDictData(file);
        return R.ok();
    }

    // http://localhost:8202/admin/cmn/dict/getName/Hostype/1    三级甲等
    @GetMapping("/getName/{parentDictCode}/{value}")
    public String getName(@PathVariable String parentDictCode,@PathVariable String value){
        String name = dictService.getNameByParentDictCodeAndValue(parentDictCode, value);
        return name;
    }


    // http://localhost:8202/admin/cmn/dict/getName/130100	石家庄市
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        String name = dictService.getNameByParentDictCodeAndValue("", value);
        return name;
    }

    /**
     * 根据dictCode查询Dict的下级子节点
     */
    @GetMapping("/findByDictCode/{dictCode}")
    public R findByDictCode(@PathVariable String dictCode){
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return R.ok().data("list",dictList);
    }
}
